// pages/business/tareweight/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: "",
        grossWeight: "",
        carWeight: "",
        secondPeelingType: "1",
        showQualityPicker: false,
        qualityType: null,
        selectCropItem: {},
        qualityMap: {
            1: "精品",
            2: "次品",
            3: "统货",
        },
        typeList: [{
                packId: 2,
                packName: "筐",
                packNumber: "",
                packWeight: "",
            }
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.id) {
            this.setData({
                id: options.id,
            })
            this.getData(this.data.id)
        } else {
            const eventChannel = this.getOpenerEventChannel()
            eventChannel.on('acceptDataFromOpenerPage', (data) => {
                this.setData({
                    qualityType: data.qualityType,
                    selectCropItem: data.selectCropItem,
                    secondPeelingType: data.secondPeelingType || "1",
                })
                if (data.typeList && data.typeList.length) {
                    let typeList = this.data.typeList
                    typeList = typeList.map(item => {
                        for (let i = 0; i < data.typeList.length; i++) {
                            if (item.packId == data.typeList[i].packId) {
                                item = data.typeList[i]
                            }
                        }
                        return item
                    })
                    this.setData({
                        typeList: typeList,
                    })
                }
            })
        }
    },

    onChange(event) {
        this.setData({
            secondPeelingType: event.detail,
        });
    },

    onNumberChange: function (e) {
        let value = e.detail;
        let index = e.currentTarget.dataset.index
        let typeList = this.data.typeList
        typeList[index].packNumber = value
        this.setData({
            typeList: typeList
        })
    },

    onWeightChange: function (e) {
        let value = e.detail;
        let index = e.currentTarget.dataset.index
        let typeList = this.data.typeList
        typeList[index].packWeight = value
        this.setData({
            typeList: typeList
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    getData(id) {
        wx.showLoading({
            title: "正在请求数据中"
        })
        wx.$apis.getMarketDetail(id).then(res => {
            this.setData({
                selectCropItem: {
                    cropName: res.data.cropName
                },
                grossWeight: res.data.grossWeight >= 0 ? res.data.grossWeight : "",
                carWeight: res.data.carWeight >= 0 ? res.data.carWeight : "",
            })
        })
    },

    submit: function () {
        let typeList = this.data.typeList;
        let tareWeight = 0;
        let tareNumber = 0;
        typeList.forEach(item => {
            tareWeight += item.packWeight * 1
            tareNumber += item.packNumber * 1
        });
        if (!tareWeight) {
            wx.showToast({
                title: "请输入二次皮重",
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (!tareNumber) {
            wx.showToast({
                title: "请输入数量",
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.id) {
            let netWeight = this.data.grossWeight - this.data.carWeight - tareWeight
            if (netWeight < 0) {
                wx.showToast({
                    title: "商品净重小于0，请检查除皮重量",
                    icon: 'none',
                    duration: 2000
                })
                return
            }
            let params = {
                "carWeight": this.data.carWeight,
                "grossWeight": this.data.grossWeight,
                "id": this.data.id,
                "packs": typeList.filter(item => item.packWeight > 0),
                "secondPeelingType": this.data.secondPeelingType,
                "qualityType": this.data.qualityType,
                "tareWeight": tareWeight
            }
            wx.$apis.updateEnterStock(params).then(res => {
                wx.showToast({
                    title: "二次除皮成功",
                    icon: 'none',
                    duration: 2000
                })
                wx.setStorage({
                    key: "back",
                    data: true,
                    success: function () {
                        wx.navigateBack(); //返回上一个页面
                    }
                })
            })
        } else {
            wx.setStorage({
                key: "tareWeightType",
                data: {
                    typeList: typeList,
                    qualityType: this.data.qualityType,
                    secondPeelingType: this.data.secondPeelingType
                },
                success: function () {
                    wx.navigateBack(); //返回上一个页面
                }
            })
        }
    },
})